package lex;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import lex.Token;

public class outputFactory {

        private List<Token> tokenSource;
        private String path;
        FileWriter outputStream;
        
        /** Constructor for a new outputFactory*/
        public outputFactory(List<Token> source, String inputPath){
                this.tokenSource = source;
                this.path = modifyPath(inputPath);
                
        }
        
        private String modifyPath(String input){
                String outputPath = input.substring(0, input.lastIndexOf('.')) + ".tok";
                return outputPath;
        }
        
        /** Create output file */
        public void makeOutputFile() throws IOException{
                try {
                /*Create output file*/
                                        
                outputStream = new FileWriter(this.path);
                Iterator<Token> tokenIter = tokenSource.iterator();
                                
                /*Write to file*/
                outputStream.write("token" + "  " + "tag" + "   " + "line : column\n");
                String outputString = null;
                while (tokenIter.hasNext()) {
                	Token nextToken = tokenIter.next();
                        if(nextToken.errorCode==0){
                                outputString = nextToken.text + "       " +
                                                                nextToken.tag + "       " + 
                                                                nextToken.line + "      " +     
                                                                nextToken.start;
                        }else{
                                outputString = nextToken.line + ":" +nextToken.start + " : lexical error; ";
                                switch (nextToken.errorCode) {
	                            case 1:  outputString += "unterminated comment";
	                                     break;
	                            case 2:  outputString += "an identifier cannot start with '_'";
	                                     break;
	                            case 3:  outputString += "invalid character '" + nextToken.text.charAt(0) + "'"; 
	                                     break;
	                            case 4:  outputString += "malformed string literal";
	                            		 break;
	                            case 5:  outputString += "INTEGER cannot start with '0'";
	                            break;
	                            default: outputString += "Default error";
                    
                                }
                            }
                        
                        outputStream.write(outputString + "\n");
                    }
                        
                        } finally {
                    if (outputStream != null) {
                        outputStream.close();
                    }
                }
        }
        
}